package com.wchen.jvm;

import java.util.ArrayList;
import java.util.List;

public class HeapOutOfMemoryError {

    static class OOMObject{}
    private static final int _1MB = 1024 * 1024;

    public static void main(String[] args) {
//        HeapOutOfMemoryError heapOutOfMemoryError = new HeapOutOfMemoryError();
        byte[] allocation1, allocation2, allocation3, allocation4, allocation5, allocation6;
        allocation1 = new byte[1 * _1MB];
        allocation2 = new byte[1 * _1MB];
        allocation3 = new byte[1 * _1MB];
        allocation3 = new byte[4 * _1MB];
        allocation3 = new byte[4 * _1MB];
        allocation4 = new byte[4 * _1MB];
        allocation5 = new byte[4 * _1MB];
        allocation6 = new byte[4 * _1MB];

//        heapOutOfMemoryError.HeapOutOfMemoryErrorTest();
    }


    public void HeapOutOfMemoryErrorTest(){
        List oomObjects = new ArrayList<>();

        while (true){
            oomObjects.add(new byte[2*_1MB]);
//            oomObjects.add(new OOMObject());
            System.out.println(oomObjects.size());
        }
    }

}
